home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / sighandlers.h < prev    next >
C/C++ Source or Header  |  1997-03-07  |  5KB  |  203 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. // This file should always be included after config.h!
  24.  
  25. /* Modified by Klaus Gebhardt, 1996 */
  26.  
  27. #if !defined (octave_sighandlers_h)
  28. #define octave_sighandlers_h 1
  29.  
  30. #include <string>
  31.  
  32. #include <Array.h>
  33.  
  34. #include "syswait.h"
  35.  
  36. // Signal handler return type.
  37. #ifndef RETSIGTYPE
  38. #define RETSIGTYPE void
  39. #endif
  40. #ifndef BADSIG
  41. #define BADSIG (RETSIGTYPE (*)(int))-1
  42. #endif
  43.  
  44. // The following signal blocking stuff is stolen from bash:
  45.  
  46. #define BLOCK_SIGNAL(sig, nvar, ovar) \
  47.   do \
  48.     { \
  49.       sigemptyset (&nvar); \
  50.       sigaddset (&nvar, sig); \
  51.       sigemptyset (&ovar); \
  52.       sigprocmask (SIG_BLOCK, &nvar, &ovar); \
  53.     } \
  54.   while (0)
  55.  
  56. #if defined (HAVE_POSIX_SIGNALS)
  57. #define BLOCK_CHILD(nvar, ovar) BLOCK_SIGNAL (SIGCHLD, nvar, ovar)
  58. #define UNBLOCK_CHILD(ovar) sigprocmask (SIG_SETMASK, &ovar, 0)
  59. #else
  60. #define BLOCK_CHILD(nvar, ovar) ovar = sigblock (sigmask (SIGCHLD))
  61. #define UNBLOCK_CHILD(ovar) sigsetmask (ovar)
  62. #endif
  63.  
  64. typedef RETSIGTYPE sig_handler (int);
  65.  
  66. struct
  67. octave_interrupt_handler
  68. {
  69. #ifdef SIGINT
  70.   sig_handler *int_handler;
  71. #endif
  72.  
  73. #ifdef SIGBREAK
  74.   sig_handler *brk_handler;
  75. #endif
  76. };
  77.  
  78. // Nonzero means we have already printed a message for this series of
  79. // SIGPIPES.  We assume that the writer will eventually give up.
  80. extern int pipe_handler_error_count;
  81.  
  82. // Nonzero means we can be interrupted.
  83. extern int can_interrupt;
  84.  
  85. extern sig_handler *octave_set_signal_handler (int, sig_handler *);
  86.  
  87. extern void install_signal_handlers (void);
  88.  
  89. extern octave_interrupt_handler octave_catch_interrupts (void);
  90.  
  91. extern octave_interrupt_handler octave_ignore_interrupts (void);
  92.  
  93. extern octave_interrupt_handler 
  94. octave_set_interrupt_handler (RETSIGTYPE (*)(int));
  95.  
  96. extern octave_interrupt_handler 
  97. octave_set_interrupt_handler (const volatile octave_interrupt_handler&);
  98.  
  99. extern void octave_save_signal_mask (void);
  100.  
  101. extern void octave_restore_signal_mask (void);
  102.  
  103. // extern void ignore_sigchld (void);
  104.  
  105. // This is taken directly from Emacs 19:
  106.  
  107. #ifndef SYS_SIGLIST_DECLARED
  108. extern char *sys_siglist[];
  109. #endif
  110.  
  111. // Maybe this should be in a separate file?
  112.  
  113. class
  114. octave_child
  115. {
  116. public:
  117.  
  118.   typedef void (*dead_child_handler) (pid_t, int);
  119.  
  120.   octave_child (pid_t id = -1, dead_child_handler f = 0, string name = "")
  121.     : pid (id), handler (f), filename (name) { }
  122.  
  123.   octave_child (const octave_child& oc)
  124.     : pid (oc.pid), handler (oc.handler), filename (oc.filename) { }
  125.  
  126.   octave_child& operator = (const octave_child& oc)
  127.     {
  128.       if (&oc != this)
  129.     {
  130.       pid = oc.pid;
  131.       handler = oc.handler;
  132.       filename = oc.filename;
  133.     }
  134.       return *this;
  135.     }
  136.  
  137.   ~octave_child (void) { }
  138.  
  139.   // The process id of this child.
  140.   pid_t pid;
  141.  
  142.   // The function we call if this child dies.
  143.   dead_child_handler handler;
  144.  
  145.   // The file to remove if this child dies.
  146.   string filename;
  147. };
  148.  
  149. class
  150. octave_child_list
  151. {
  152. protected:
  153.  
  154.   octave_child_list (void) : list (0), curr_len (0) { }
  155.  
  156. public:
  157.  
  158.   ~octave_child_list (void) { }
  159.  
  160.   static void insert (pid_t pid, octave_child::dead_child_handler f,
  161.               string name = "");
  162.  
  163.   static void remove (pid_t pid);
  164.  
  165.   static int length (void) { return instance ? instance->curr_len : 0; }
  166.  
  167.   static octave_child& elem (int i)
  168.     {
  169.       static octave_child foo;
  170.  
  171.       if (instance)
  172.     {
  173.       int n = length ();
  174.  
  175.       if (i >= 0 && i < n)
  176.         return instance->list (i);
  177.     }
  178.  
  179.       return foo;
  180.     }
  181.  
  182. private:
  183.  
  184.   Array<octave_child> list;
  185.  
  186.   int curr_len;
  187.  
  188.   static octave_child_list *instance;
  189.  
  190.   void do_insert (pid_t pid, octave_child::dead_child_handler f,
  191.           string name = "");
  192.  
  193.   void do_remove (pid_t pid);
  194. };
  195.  
  196. #endif
  197.  
  198. /*
  199. ;;; Local Variables: ***
  200. ;;; mode: C++ ***
  201. ;;; End: ***
  202. */
  203.